package com.slj.controller;

import com.slj.comm.responce.PageResult;
import com.slj.comm.responce.R;
import com.slj.model.entity.Inventory;
import com.slj.model.vo.InventoryProductVO;

import com.slj.service.InventoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/inventory")
@Tag(name = "库存管理", description = "库存增删改查接口")
@CrossOrigin(origins = "*")
public class InventoryController {

    @Autowired
    private InventoryService inventoryService;

    @Operation(summary = "新增库存")
    @PostMapping("/add")
    public R<String> add(@RequestBody Inventory inventory) {
        return inventoryService.addInventory(inventory);
    }

    @Operation(summary = "删除库存")
    @DeleteMapping("/delete/{id}")
    public R<String> delete(@PathVariable("id") Integer id) {
        return inventoryService.deleteInventory(id);
    }

    @Operation(summary = "更新库存")
    @PutMapping("/update")
    public R<String> update(@RequestBody Inventory inventory) {
        return inventoryService.updateInventory(inventory);
    }

    @Operation(summary = "根据ID查询库存")
    @GetMapping("/get/{id}")
    public R<Inventory> getById(@PathVariable("id") Integer id) {
        return inventoryService.getInventoryById(id);
    }

    @Operation(summary = "分页查询库存列表")
    @GetMapping("/page")
    public R<List<InventoryProductVO>> page(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "productName", required = false) String productName) {
        return inventoryService.getInventoryPage(pageNum, pageSize, productName);
    }

    @Operation(summary = "调整安全库存")
    @PutMapping("/adjust-safe-quantity")
    public R<String> adjustSafeQuantity(
            @RequestParam("inventoryId") Integer inventoryId,
            @RequestParam("safeQuantity") Integer safeQuantity) {
        return inventoryService.adjustSafeQuantity(inventoryId, safeQuantity);
    }

    @Operation(summary = "导出库存数据到Excel")
    @GetMapping("/export")
    public void exportInventory(HttpServletResponse response) {
        inventoryService.exportInventoryToExcel(response);
    }

    @Operation(summary = "批量更新库存")
    @PutMapping("/batch-update")
    public R<String> batchUpdate(@RequestBody List<Inventory> inventoryList) {
        return inventoryService.batchUpdateInventory(inventoryList);
    }

    @Operation(summary = "获取低于安全库存的商品列表")
    @GetMapping("/below-safe-quantity")
    public R<List<Inventory>> getBelowSafeQuantity() {
        return inventoryService.getInventoryBelowSafeQuantity();
    }

    @Operation(summary = "分页查询库存商品信息")
    @GetMapping("/product-page")
    public R<PageResult<InventoryProductVO>> getInventoryProductPage(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "productName", required = false) String productName) {
        return inventoryService.getInventoryProductPage(pageNum, pageSize, productName);
    }

    @Operation(summary = "获取低于安全库存的商品信息")
    @GetMapping("/product-below-safe")
    public R<List<InventoryProductVO>> getInventoryProductBelowSafeQuantity() {
        return inventoryService.getInventoryProductBelowSafeQuantity();
    }
}